
import { _decorator, Component, Node,Vec3, RigidBody, Collider, log } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('Ball')
export class Ball extends Component {
    private initSpeed=0;

    protected onLoad(): void {
        
        let collider=this.getComponent(Collider)
        if(collider){
            collider.on("onCollisionEnter",this.ontoucheach,this)
        }
    }


    //反弹的速度限制
    bounce(){
        let rigidBody = this.node.getComponent(RigidBody)
        if(this.initSpeed==0){
            if(rigidBody){
                let vc=new Vec3(0,0,0)
                rigidBody.getLinearVelocity(vc)
                this.initSpeed=vc.y   //记录速度
            }
        }else{
            if(rigidBody){
                //设置速度
                rigidBody.setLinearVelocity(new Vec3(0,this.initSpeed,0))
                
            }
        }
        
    }

    ontoucheach(){
        this.bounce()
        console.log("破撞");
        
    }

    Ballmove(){
        let rigidBody = this.node.getComponent(RigidBody)
        if (rigidBody) {
            rigidBody.setLinearVelocity(new Vec3(0, -4, 0))
            console.log("zsh");
        }
    }
}

